package com.hdjk.view.callback;

import com.hdjk.view.entity.IntegralEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class GetIntegralListCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg = super.parseNetworkResponse(response, i);
        JSONArray array = (JSONArray) msg.getData();
        if (array != null && array.length() > 0) {
            List<IntegralEntity> list = new ArrayList<>();
            for (int j = 0; j < array.length(); j++) {
                JSONObject jsonObject = array.getJSONObject(j);
                IntegralEntity entity = new IntegralEntity();
                if (!jsonObject.isNull("UserId")) {
                    entity.setUserId(jsonObject.getInt("UserId"));
                }
                if (!jsonObject.isNull("Credit")) {
                    entity.setCredit(jsonObject.getInt("Credit"));
                }
                if (!jsonObject.isNull("ChangeCredit")) {
                    entity.setChangeCredit(jsonObject.getInt("ChangeCredit"));
                }
                if (!jsonObject.isNull("Type")) {
                    entity.setType(jsonObject.getInt("Type"));
                }
                if (!jsonObject.isNull("Title")) {
                    entity.setTitle(jsonObject.getString("Title"));
                }
                if (!jsonObject.isNull("Remark")) {
                    entity.setRemark(jsonObject.getString("Remark"));
                }
                if (!jsonObject.isNull("Tag")) {
                    entity.setTag(jsonObject.getString("Tag"));
                }
                if (!jsonObject.isNull("CreateTime")) {
                    entity.setCreateTime(jsonObject.getString("CreateTime"));
                }
                if (!jsonObject.isNull("Id")) {
                    entity.setId(jsonObject.getString("Id"));
                }
                list.add(entity);
            }
            msg.setData(list);
        }else {
            msg.setData(null);
        }
        return msg;
    }
}
